package Dmeo2_22;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User：Lenovo
 * Date:2025-02-22
 * Time:18:59
 */
import java.util.HashMap;
import java.util.Map;

public class MapTraversal {
    public static void main(String[] args) {
        Map<String, Integer> scores = new HashMap<>();
        scores.put("Alice", 85);
        scores.put("Bob", 90);
        scores.put("Charlie", 78);

        // 遍历键
        for (String name : scores.keySet()) {
            System.out.println("Name: " + name);
        }

        // 遍历值
        for (int score : scores.values()) {
            System.out.println("Score: " + score);
        }

        // 遍历键值对
        for (Map.Entry<String, Integer> entry : scores.entrySet()) {
            System.out.println("Name: " + entry.getKey() + ", Score: " + entry.getValue());
        }

        // 使用 forEach 方法遍历键值对
        scores.forEach((name, score) -> System.out.println("Name: " + name + ", Score: " + score));
    }
}
